package com.itheima.controller;

import com.itheima.domain.Teacher;
import com.itheima.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController {
    Scanner sc=new Scanner(System.in);
    TeacherService teaSer=new TeacherService();
    public void start() {
        while(true){
            System.out.println("------欢迎来到黑马老师管理系统------");
            System.out.println("1 新增老师");
            System.out.println("2 删除老师");
            System.out.println("3 修改老师");
            System.out.println("4 查看老师");
            System.out.println("5 退出");
            System.out.println("请输入您的选择：");
            // 接收用户的选择
            String choose = sc.next();
            switch (choose) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    searchTeacher();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("项目展示没有开通~");
                    break;
            }
        }
    }

    private void deleteTeacher() {
        System.out.println("请输入您要删除的工号：");
        String id = sc.next();
        boolean exists = teaSer.exists(id);
        if(exists){
            System.out.println("未找到id为" + id + "的老师！");
            return;
        }
        teaSer.deleteTeacher(id);
        System.out.println("删除成功！！");
    }

    private void updateTeacher() {
        System.out.println("请输入您要修改新工号：");
        String id = sc.next();
        boolean exist = teaSer.exists(id);
        if(exist){
            System.out.println("未找到id为" + id + "的老师！");
            return;
        }
        System.out.println("请输入您的新姓名：");
        String name = sc.next();
        System.out.println("请输入您的新年龄：");
        String age = sc.next();
        System.out.println("请输入您的新籍贯：");
        String address = sc.next();
        // 创建技师对象
        Teacher tec = new Teacher(id, name, age, address);
        // 将数据传递给业务员
        teaSer.updateTeacher(tec);
        System.out.println("老师修改成功！");
    }

    private void searchTeacher() {
        ArrayList<Teacher>list=teaSer.searchTeacher();
        if(list.size()==0&&list.isEmpty()){
            System.out.println("查无信息，请添加后再查询。");
            return;
        }
        System.out.println("工号\t\t姓名\t年龄\t出生地址");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getAge() + "\t" + list.get(i).getBrithday());
        }
    }

    private void addTeacher() {
        String id;
         lo:while (true){
             System.out.println("请输入您的工号：");
             id = sc.next();
             boolean flag=teaSer.exists(id);
             if(flag){
                 break lo;
             }
             System.out.println("你输入的工号,已被占用");
         }
        System.out.println("请输入您的姓名：");
        String name = sc.next();
        System.out.println("请输入您的年龄：");
        String age = sc.next();
        System.out.println("请输入您的地址：");
        String address = sc.next();
        Teacher tea=new Teacher(id,name,age,address);
        teaSer.addTeacher(tea);
        System.out.println("添加成功！！");
    }
}


